//
//  LHPublicMethods.m
//  lhcx
//
//  Created by 赵永杰 on 2017/5/9.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "LHPublicMethods.h"


extern BOOL LHJudgeIsTestAccount(NSString * mobile){
    
    NSArray * testMobile = [HLSharedUserDefault objectForKey:LHTestAccount];
    
    for (NSString * str in testMobile) {
        if ([str isEqualToString:mobile]) {
            return YES;
        }
    }
    
    return NO;
}

UIViewController *LHGetSB_VC(NSString *name) {
    return [[UIStoryboard storyboardWithName:name bundle:nil] instantiateInitialViewController];
}

extern NSString *LHSetRequestAPI(NSString *path) {
    return [NSString stringWithFormat:@"%@%@",LHBaseURL,path];
}

extern NSString *LHSetH5RequestAPI(NSString *path) {
    return [NSString stringWithFormat:@"%@%@",LHH5BaseURL,path];
}

extern NSString *LHGetUserToken() {
    NSString *token = [HLSharedUserDefault objectForKey:LHUserToken];
    return token;
}

extern NSString *LHGetUserID() {
    NSString *token = [HLSharedUserDefault objectForKey:LHUserID];
    return token ? token : @"11";
}

extern void LHSetUserToken(NSString *token) {
    [HLSharedUserDefault setObject:token forKey:LHUserToken];
    [HLSharedUserDefault synchronize];
}

extern void LHSetUserID(NSString *userid) {
    [HLSharedUserDefault setObject:userid forKey:LHUserID];
    [HLSharedUserDefault synchronize];
}

extern void LHSetUserPhone(NSString *phone) {
    [HLSharedUserDefault setObject:phone forKey:LHUserPhone];
    [HLSharedUserDefault synchronize];
}

extern NSString * LHGetUserPhone() {
    return [HLSharedUserDefault valueForKey:LHUserPhone];
}

extern void LHSetLoginStatus(BOOL login) {
    [HLSharedUserDefault setBool:login forKey:@"lhcx_login"];
    [HLSharedUserDefault synchronize];
}

extern BOOL LHGetLoginStatus() {
    return [HLSharedUserDefault boolForKey:@"lhcx_login"];
}

extern NSString *LHGetTimeString(NSDate *date, NSString *dateFormatStr) {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = dateFormatStr;
    return [format stringFromDate:date];
}

NSString *LHGetReserveDay(NSArray *array,NSInteger row) {
    if (array.count == 3) {
        if (row == 0) {
            return @"今天";
        }else if (row == 1) {
            return @"明天";
        }else{
            return array[2];
        }
    }else{
        if (row == 0) {
            return @"明天";
        }else{
            return array[1];
        }
    }
}

UIViewController *LHGetPresentingVC() {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}

void LHCallPhone(NSString *phone) {
    if (phone.length >= 10) {
        NSString *str2 = [[UIDevice currentDevice] systemVersion];
        if ([str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedDescending || [str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedSame)
        {
            NSString* PhoneStr = [NSString stringWithFormat:@"telprompt://%@",phone];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:PhoneStr] options:@{} completionHandler:^(BOOL success) {
                NSLog(@"phone success");
            }];
            
        }else {
            NSMutableString* str1 = [[NSMutableString alloc]initWithString:phone];// 存在堆区，可变字符串
            if (phone.length == 10) {
                [str1 insertString:@"-"atIndex:3];// 把一个字符串插入另一个字符串中的某一个位置
                [str1 insertString:@"-"atIndex:7];// 把一个字符串插入另一个字符串中的某一个位置
            }else {
                [str1 insertString:@"-"atIndex:3];// 把一个字符串插入另一个字符串中的某一个位置
                [str1 insertString:@"-"atIndex:8];// 把一个字符串插入另一个字符串中的某一个位置
            }
            NSString * str = [NSString stringWithFormat:@"是否拨打电话\n%@",str1];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message: nil preferredStyle:UIAlertControllerStyleAlert];
            // 设置popover指向的item
//            alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
            // 添加按钮
            [alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                NSLog(@"点击了呼叫按钮10.2下");
                NSString* PhoneStr = [NSString stringWithFormat:@"tel://%@",phone];
                //                if ([PhoneStr hasPrefix:@"sms:"] || [PhoneStr hasPrefix:@"tel:"]) {
                //
                //                }
                UIApplication * app = [UIApplication sharedApplication];
                if ([app canOpenURL:[NSURL URLWithString:PhoneStr]]) {
                    [app openURL:[NSURL URLWithString:PhoneStr]];
                }
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"点击了取消按钮");
            }]];
            [LHGetPresentingVC() presentViewController:alert animated:YES completion:nil];
        }
    }
}

void LHSetMapShowTraffic(BOOL isShow) {
    [HLSharedUserDefault setBool:isShow forKey:@"showTraffic"];
    [HLSharedUserDefault synchronize];
}

BOOL LHGetMapTrafficIsShow() {
    return [HLSharedUserDefault boolForKey:@"showTraffic"];
}

NSString *LHGetAge(NSString *date) {
    NSString *a = [date substringWithRange:NSMakeRange(1, 2)];
    NSString *result = @"长者";
    if ([a isEqualToString:@"01"]) {
        result = @"10后";
    }else if ([a isEqualToString:@"00"]){
        result = @"00后";
    }else if ([a isEqualToString:@"99"]) {
        result = @"90后";
    }else if ([a isEqualToString:@"98"]) {
        result = @"80后";
    }else if ([a isEqualToString:@"97"]) {
        result = @"70后";
    }else if ([a isEqualToString:@"96"]) {
        result = @"60后";
    }else if ([a isEqualToString:@"95"]) {
        result = @"50后";
    }
    return result;
    
    return a;
}


